package cn.edu.nbpt.campus_api.controller.blog;

import cn.edu.nbpt.campus_api.model.Page;
import cn.edu.nbpt.campus_api.model.Result;
import cn.edu.nbpt.campus_api.model.blog.entity.Category;
import cn.edu.nbpt.campus_api.service.blog.CategoryService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("blog/category")
public class CategoryController {

    @Resource
    private CategoryService categoryService;

    @GetMapping("list")
    public Result<Page<Category>> selectCategoryList(
            @RequestParam(required = false) String name,
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(required = false, defaultValue = "10") Integer pageSize
    ) {
        return Result.success(categoryService.selectCategoryList(name, pageNum, pageSize));
    }

    @GetMapping("{id}")
    public Result<Category> selectCategory(
            @PathVariable Integer id
    ) {
        return Result.success(categoryService.selectCategory(id));
    }

    @PostMapping
    public Result<Integer> insertCategory(
            @RequestBody Category category
    ) {
        return Result.success(categoryService.insertCategory(category));
    }

    @PutMapping
    public Result<Integer> updateCategory(
            @RequestBody Category category
    ) {
        return Result.success(categoryService.updateCategory(category));
    }

    @DeleteMapping("{ids}")
    public Result<Integer> deleteCategory(
            @PathVariable List<Integer> ids
    ) {
        return Result.success(categoryService.deleteCategory(ids));
    }
}
